<?
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) . "/../t2" );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

$mainframe->route();
$user =& JFactory::getUser();


$this_username = $user->username;
$this_useremail = $user->email;
$this_userid = $user->id;
$this_usertype = $user->usertype;

require_once "../web4/dbconfigpublic.php";

try {
        $curr_roomid=intval($_GET["roomid"]);
}
catch (Exception $e )
{
        exit;
}

$query = "SELECT *,date_format(nextevent,\"%M %e %h%p\")as ne1 from discussroom where published=1 and id=$curr_roomid";
$result = mysql_query($query,$db) or die (" died on query: ". $query);
$row_count = mysql_num_rows ( $result);
if ( $row_count <=0 )
	exit;
$myrow = mysql_fetch_array($result);

if ( $myrow["online"] == 1 )
{
	$status="online";
}
else 
	$status="offline";
$ne = explode ( " ", $myrow["ne1"], 3);


function SendCommandSF($command,$para1,$para2,$para3 ) {
    switch ($command)
    {
	case "SENDEMAIL":
		return SendEmailSF(urldecode($para1), urldecode($para2),urldecode($para3));	
		break;
	case "NE":
		return NewEventSF($para1);	
		break;
	case "CS":
		return ChangeStatusSF($para1);	
		break;
    }
    return "OK";
}

function ChangeStatusSF($mode )
{
	global $this_username;
	global $this_userid;
	global $curr_roomid;
	global $db;
	if ($this_username =="" )
	{
		return "NOTLOGIN";
	}
	$config = array ();
	switch ( $mode )
	{
		case "0":
			$query = sprintf("update discussroom set online=%d,token='',sessionid='',sessionstarted=current_timestamp where id=%d ",
				$mode,$curr_roomid);
			break;
		case "1":
			include "config.php";
			require_once "lib/tb_functions.php";
			updateToken( $config);
			$query = sprintf("update discussroom set online=%d,token='%s',sessionid='%s',sessionstarted=current_timestamp where id=%d ",
				$mode,$config["TOKEN" ],$config["SESSIONID"],$curr_roomid);
			break;
	} 
	
	try  {
		$result = mysql_query("$query",$db) ;
	}
 	catch (Exception $e)
	{
		return "CSFAILED";
	}
	return "CSOK";
}

function NewEventSF($newdate)
{
	global $this_username;
	global $this_userid;
	global $curr_roomid;
	global $db;
	if ($this_username =="" )
	{
		return "NOTLOGIN";
	}
	
	$query = "update discussroom set nextevent = concat(date_format( current_date,'%Y') , '-','$newdate') where id=$curr_roomid;";
	try 
	{
		$result = mysql_query($query,$db) ;
	}
	catch ( exception $e )
	{
		return "NEFAILED";
	}
	return "NEOK";
}
function SendEmailSF($name, $email,$phone )
{
	global $myrow;
	global $db;

        require("../web4/classes/phpmailer/class.phpmailer.php");

        $mail = new PHPMailer();

	$group=$myrow["roomname"];
        $mail->FromName = "The RealNews";
        $mail->From = "info@therealnews.com";

        $mail->Subject = "Discussion Group $group RSVP";
        $thisemail = "jack.chen.job@gmail.com";
        $mail->AddAddress($thisemail);
        $mail->AddAddress( 'hezvo@therealnews.com' );


        $mail->Body    =  "Group: $group\r\nName: $name \r\nEmail: $email \r\nPhone: $phone \r\n";
	if($mail->Send())
		return "SENDEMAILOK";
	else
		return "SENDEMAILFAILED";
}

    require("../web4/sajax/sajax.php");

    $sajax_failure_redirect = "http://sajax.info/sajaxfail.html";
    sajax_export("SendCommandSF");
    sajax_handle_client_request();



?>
<style type="text/css">
<!--
body,td,th {
	font-family: Arial, Helvetica, sans-serif;
}
-->
</style>


<script type="text/javascript" src="/web4/sajax/json2.stringify.js"></script>
<script type="text/javascript" src="/web4/sajax/json_stringify.js"></script>
<script type="text/javascript" src="/web4/sajax/json_parse_state.js"></script>
<script type="text/javascript" src="/web4/sajax/sajax.js"></script>

<script>

<?
	sajax_show_javascript();
?>
function FIND(item) {
   if( window.mmIsOpera ) return(document.getElementById(item));
   if (document.all) return(document.all[item]);
   if (document.getElementById) return(document.getElementById(item));
   return(false);
};

function SendCommand_callback( result) {
	
        if ( result == "SENDEMAILFAILED" )
        {
		mymessage.innerHTML="RSVP submitting failed";
        }
        if ( result == "SENDEMAILOK" )
        {
		myname.value="";
		myemail.value="";
		myphone.value="";
		mymessage.innerHTML="RSVP submitted";
        }
        if ( result == "NEFAILED" )
        {
		myadminmessage.innerHTML="New Event date submitting failed";
        }
        if ( result == "NEOK" )
        {
		myadminmessage.innerHTML="New Event date submitting OK";
		window.location.reload();
        }
        if ( result.indexOf( "CSFAILED" )== 0  )
        {
		myadminmessage.innerHTML="changing status failed:" + result;
        }
        if ( result == "CSOK" )
        {
		myadminmessage.innerHTML="changing status OK";
		window.location.reload();
        }
}

function SendCommand(command, para1,para2,para3)
{
        x_SendCommandSF(command, para1,para2,para3, SendCommand_callback);
}

function ChangeStatus( mode  )
{
	SendCommand("CS",mode,"","");
}
function SaveNextEvent( )
{
	month= nemonth.options[nemonth.selectedIndex].value;
	if ( month =="Month" )
	{
		myadminmessage.innerHTML="please select a month.";
		return;
	}
	day= neday.options[neday.selectedIndex].value;
	if ( day =="Day" )
	{
		myadminmessage.innerHTML="please select a day.";
		return;
	}
	hour= nehour.options[nehour.selectedIndex].value;
	if ( hour =="Time" )
	{
		myadminmessage.innerHTML="please select a hour.";
		return;
	}
	newdate = month + "-" + day + " " + hour;
	SendCommand("NE",newdate,"","");
}


function SendEmail()
{
	if ( myname.value =="" )
	{
		mymessage.innerHTML="please type in a name, then submit.";
		return;
	}
	if ( myemail.value =="" || (validateEmail(myemail.value) == false ) )
	{
		mymessage.innerHTML="please type in a valid email address, then submit.";
		return;
	}
	if ( myphone.value =="" )
	{
		mymessage.innerHTML="please type in a phone number, then submit.";
		return;
	}
	SendCommand("SENDEMAIL",encodeURI (myname.value),encodeURI (myemail.value), encodeURI (myphone.value)  );
}
function validateEmail ( address ) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(address) == false) {
      return false;
   }
   return true;
}

</script>

<!--<center><h2>You are now in the <?=$myrow["roomname"]?> discussion group</h2></center>
-->
<? if ( $status == offline )  { ?>
<center><h3>This group is currently offline</h3></center>
<center>The next discussion will be held on <b><? echo $ne[0]." ".$ne[1]?></b> at <b><?=$ne[2]?></b> Use the form below to RSVP to this event.
<p>

Name: <input type="text" id="myname" maxlength="50"/>
Email: <input type="text" id="myemail" maxlength="100"/>
Phone: <input type="text" id="myphone" maxlength="20"/>
<input type="button" onclick="Javascript:SendEmail()" value="RSVP" />
<div id=mymessage></div>

<?} else {    //online               
	echo "<iframe width=900 frameborder=0 height=850 src=publish_db.php?roomid=$curr_roomid></iframe>";
}

if ( $this_usertype =="Administrator" ||  $this_usertype =="Super Administrator") {
?>
<center><h2>Administrator Options</h2></center>
<center>Discussion room 
<? 
if ( $status == "offline" ) 
	print "<a href=\"javascript:ChangeStatus(1);\">Online</a>";
else
	print "<a href=\"javascript:ChangeStatus(0);\">Offline</a>";

?>
 | <a href="http://therealnews.com/tokboxnew/admin/debateshowconfig.php">Set up room</a>
</center>
<center>Change the 
<select id=nemonth>
<option value="Month">Month</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value=12"">December</option>
</select>

<select id=neday>
<option value="Day">Day</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
 and 
 
 <select id=nehour>
<option value="Time">Time</option>
<option value="10:00">10:00AM</option>
<option value="11:00">11:00AM</option>
<option value="12:00">12:00PM</option>
<option value="13:00">1:00PM</option>
<option value="14:00">2:00PM</option>
<option value="15:00">3:00PM</option>
<option value="16:00">4:00PM</option>
<option value="17:00">5:00PM</option>
<option value="18:00">6:00PM</option>
<option value="19:00">7:00PM</option>
<option value="20:00">8:00PM</option>
<option value="21:00">9:00PM</option>
<option value="22:00">10:00PM</option>

</select>
 for the next event</center>
<p>
<center><input type="button" onclick="javascript:SaveNextEvent()" value="Save" /></center>
<div id=myadminmessage></div>
<?
} // end of if user is admin.
?>
